<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/27
 * Time: 10:16
 */

namespace zhtfrontend\modules\sitepadapi\controllers;

use common\components\helpers\Fun;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\CommentInfo;
use zhtfrontend\modules\sitepadapi\components\rest\ActiveController;
use yii\db\Query;
use yii\data\ArrayDataProvider;
use yii\helpers\Json;
use yii\web\Response;

class CommentservicerController extends ActiveController
{
    public $modelClass = 'zhtfrontend\models\datas\Comment';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionFindbyteache($teache_id)
    {
        $arr = $data = $comment_info_arr = [];
        $teacher = (new Query())->select('*')->from('{{%admin}}')->where(['id' => $teache_id])->one(Fun::getSiteDb());
        $classManage = (new Query())->select('headmaster')->from('{{%arrange_course}} a')->leftJoin('{{%class_manage}} b', 'a.id=b.code')->where([
            'a.teacher_id' => $teacher['teacher_id'], 'a.site_id' => Fun::getSiteId()
        ])->all(Fun::getSiteDb());
        foreach ($classManage as $k => $v) {
            $arr[] = $v['headmaster'];
        }
        $headmaster = (new Query())->select('*')->from('{{%admin}}')->where(['in', 'id', $arr])->all(Fun::getSiteDb());
        $commentInfo = CommentInfo::find()->where(['object_type' => 3])->asArray()->all();
        foreach ($commentInfo as $kk => $vv) {
            $comment_info_arr[] = [
                'comment_info_id' => $vv['id'],
                'comment_info_title' => '该员工的' . $vv['title']
            ];
        }
        foreach ($headmaster as $kk => $vv) {
            $data[] = [
                'teacher_id' => $vv['id'],
                'title' => '针对工作人员' . $vv['name'] . ',您的评价是:',
                'content' => $comment_info_arr
            ];
        }
        return new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 20
            ],
            'sort' => [
                'attributes' => ['teacher_id']
            ]
        ]);
    }

    public function actionCreate()
    {
        $commentRelationArr = $commentInfoArr = $commentArr = $commentContent = [];
        $data = \Yii::$app->request->post();
        foreach (Json::decode($data['array']) as $dk => $dv) {
            $commentContent[$dv['teacher_id']] = $dv['content'];
            $commentInfoArr[$dv['teacher_id']] = $dv['value'][0];
        }
        foreach ($commentInfoArr as $k => $v) {
            $commentArr[] = [
                'object_type' => 3,
                'title' => $this->getAdmin($k),
                'description' => empty($commentContent[$k])?$this->getDescription($v['for_value']):$commentContent[$k],
                'object_id' => $k
            ];
        }
        $result = Fun::getSiteDb()->createCommand()->batchInsert('{{%comment}}', [
            'object_type', 'title', 'description', 'object_id'
        ], $commentArr)->execute();
        if ($result) {
            $comment = (new Query())->select('*')->from('{{%comment}}')->orderBy('id DESC')->limit($result)->all(Fun::getSiteDb());
            foreach ($comment as $key => $val) {
                $commentRelationArr[] = [
                    'comment_id' => $val['id'],
                    'comment_info_id' => $commentInfoArr[$val['object_id']]['comment_info_id'],
                    'for_value' => $commentInfoArr[$val['object_id']]['for_value']
                ];
            }
            Fun::getSiteDb()->createCommand()->batchInsert('{{%comment_relation}}', [
                'comment_id', 'comment_info_id', 'for_value'
            ], $commentRelationArr)->execute();
        }
        return $commentRelationArr;
    }


    private function getAdmin($headmaster_id)
    {
        $headmaster = (new Query())->select('*')->from('{{%admin}}')->where(['id' => $headmaster_id])->one(Fun::getSiteDb());
        $commentTitle = '针对工作人员' . $headmaster['name'] . ',您的评价是:';
        return $commentTitle;
    }

    protected function getDescription($data){
        $commentInfoData=(new Query())->select('*')->from('{{%comment_info}}')
            ->where(['object_type'=>1])->all(Fun::getSiteDb());
        $description='';
        if(isset($commentInfoData)){
            foreach($commentInfoData as $k=>$v){
                $description.='该老师的'.$v['title'].'评价是'.$data.'分;';
            }
            return $description;
        }
        return '';
    }


}